/*
 * Copyright (c) 2014, marco.tamburelli@gmail.com
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met: 
 * 
 * 1. Redistributions of source code must retain the above copyright notice, this
 *    list of conditions and the following disclaimer. 
 * 2. Redistributions in binary form must reproduce the above copyright notice,
 *    this list of conditions and the following disclaimer in the documentation
 *    and/or other materials provided with the distribution. 
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
package org.omorphdb.core.index.hash;

import org.omorphdb.core.io.Constants;
import org.omorphdb.core.value.ValueConstants;

/**
 * Represents the initial block of information in a hash index resource; which
 * tells how the index generation should be performed.
 * 
 * @author Marco Tamburelli
 */
class Statistics
{
	static final int SIZE = Constants.SIZEOF_BYTE + Constants.SIZEOF_LONG + Constants.SIZEOF_LONG;

	private final byte algorithm;
	private long filledSlotCount;
	private long collisionCount;

	/**
	 * This is a flag which is <code>true</code> when the algorithm for the hash
	 * calculation generates a long.
	 */
	final boolean COULD_BE_REBUILT;

	Statistics(byte algorithm, long filledSlotCount, long collisionCount)
	{
		this.algorithm = algorithm;
		this.filledSlotCount = filledSlotCount;
		this.collisionCount = collisionCount;

		COULD_BE_REBUILT = this.algorithm != ValueConstants._INT;
	}

	/**
	 * The byte indicating which hash function should be used.
	 * 
	 * @return
	 */
	public byte algorithm()
	{
		return this.algorithm;
	}

	/**
	 * How many slots have been filled.
	 * 
	 * @return
	 */
	public long filledSlotCount()
	{
		return this.filledSlotCount;
	}

	void notifyFilledSlot()
	{
		this.filledSlotCount++;
	}

	void notifySlotReleased()
	{
		this.filledSlotCount--;
	}

	/**
	 * Count of collisions.
	 * 
	 * @return
	 */
	public long collisionCount()
	{
		return this.collisionCount;
	}

	void notifyNewCollision()
	{
		this.collisionCount++;
	}

	void notifyRemovedCollision()
	{
		this.collisionCount--;
	}

	/**
	 * Returns <code>true</code> when the index should be rebuilt because the
	 * count of collision exceeded 2 times the count of slots.
	 * 
	 * @return
	 */
	public boolean shouldBeRebuilt()
	{
		return this.collisionCount > this.filledSlotCount * 2;
	}
}
